package com.marquisx.common.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.marquisx.common.utils.TZUtils;
import com.marquisx.tzdice.R;

public class AboutTemplate extends Activity implements OnTouchListener, OnClickListener
{
	private TextView content;
	private Button feedbackBtn;
	private Button homepageBtn;
	private Button okBtn;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about);
		findViews();
		setListeners();
		prepareViews();
	}
	
	private void findViews()
	{
		content = (TextView) findViewById(R.id.about_content);
		feedbackBtn = (Button) findViewById(R.id.feedback_btn);
		homepageBtn = (Button) findViewById(R.id.homepage_btn);
		okBtn = (Button) findViewById(R.id.ok_btn);
	}
	
	// Add listeners for button clicks
	private void setListeners()
	{
		content.setOnTouchListener(this);
		feedbackBtn.setOnClickListener(this);
		homepageBtn.setOnClickListener(this);
		okBtn.setOnClickListener(this);
	}
	
	private void prepareViews()
	{
		setTitle(R.string.about_title);
		
		String formatResult = String.format(
				getResources().getString(R.string.about_msg),
				getResources().getString(R.string.app_version_name),
				getResources().getString(R.string.app_author),
				getResources().getString(R.string.app_email),
				getResources().getString(R.string.uri_homepage)
		);
		
		content.setText(formatResult);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		finish();
		return true;
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.homepage_btn:
				openHomepage();
				break;
			case R.id.feedback_btn:
				openEmail();
				break;
			case R.id.ok_btn:
				finish();
				break;
		}
	}
	
	private void openHomepage()
	{
		Uri uri = Uri.parse(getString(R.string.uri_homepage));
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}
	
	private void openEmail()
	{
		/* Create the Intent */  
		final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  
		  
		/* Fill it with Data */  
		emailIntent.setType("plain/text");  
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{""+ getText(R.string.app_email)});  
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getText(R.string.about_email_subject));  
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getText(R.string.about_email_content)+ "\n\n" + getSystemInfo());  
		  
		/* Send it off to the Activity-Chooser */  
		startActivity(Intent.createChooser(emailIntent, "Send mail..."));  
	}
	
	public String getSystemInfo()
	{
		StringBuffer str = new StringBuffer();
		int[] resolution = TZUtils.getDeviceResolution(this);
		
		str.append("\n----")
			.append("\ntzDice Version:" ).append( getText(R.string.app_version_name))
			.append("\nDevice: ").append(android.os.Build.MODEL)
			.append("\nAndroid Version: ").append( android.os.Build.VERSION.RELEASE )
			.append("\nScreen Resolution: ").append( resolution[0] + "x" + resolution[1] );
		
		return str.toString();
	}
}
